package com.bcb.dao;

import com.bcb.model.Permission;
import my.bcb.util.DBConnection;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class PermissionDAO {
    private Connection getConnection() throws SQLException {
        return DBConnection.getConnection();
    }
    
    public List<Permission> findAll() {
        String sql = "SELECT * FROM permissions ORDER BY permission_id";
        List<Permission> permissions = new ArrayList<>();
        
        try (Connection conn = getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                permissions.add(mapResultSetToPermission(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return permissions;
    }
    
    public Permission findById(int permissionId) {
        String sql = "SELECT * FROM permissions WHERE permission_id = ?";
        Permission permission = null;
        
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, permissionId);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                permission = mapResultSetToPermission(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return permission;
    }
    
    public Permission findByName(String permissionName) {
        String sql = "SELECT * FROM permissions WHERE permission_name = ?";
        Permission permission = null;
        
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, permissionName);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                permission = mapResultSetToPermission(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return permission;
    }
    
    public boolean insert(Permission permission) {
        String sql = "INSERT INTO permissions (permission_name, permission_description) VALUES (?, ?)";
        
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            
            pstmt.setString(1, permission.getPermissionName());
            pstmt.setString(2, permission.getPermissionDescription());
            
            int affectedRows = pstmt.executeUpdate();
            
            if (affectedRows > 0) {
                ResultSet generatedKeys = pstmt.getGeneratedKeys();
                if (generatedKeys.next()) {
                    permission.setPermissionId(generatedKeys.getInt(1));
                }
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }
    
    public boolean update(Permission permission) {
        String sql = "UPDATE permissions SET permission_name = ?, permission_description = ? WHERE permission_id = ?";
        
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, permission.getPermissionName());
            pstmt.setString(2, permission.getPermissionDescription());
            pstmt.setInt(3, permission.getPermissionId());
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }
    
    public boolean delete(int permissionId) {
        String sql = "DELETE FROM permissions WHERE permission_id = ?";
        
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, permissionId);
            
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return false;
    }
    
    private Permission mapResultSetToPermission(ResultSet rs) throws SQLException {
        Permission permission = new Permission();
        permission.setPermissionId(rs.getInt("permission_id"));
        permission.setPermissionName(rs.getString("permission_name"));
        permission.setPermissionDescription(rs.getString("permission_description"));
        return permission;
    }
}